﻿Imports System.ComponentModel
Imports System
Imports System.Reflection
Imports System.ComponentModel.Design.Serialization

Friend Class ErrMessagePropertiesConverter
   Inherits ExpandableObjectConverter

   Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, _
                                       ByVal culture As Globalization.CultureInfo, _
                                       ByVal value As Object, _
                                       ByVal destinationType As Type) As Object

      If destinationType Is GetType(String) Then
         Return String.Empty
      End If

      If destinationType Is GetType(InstanceDescriptor) Then
         Dim ci As ConstructorInfo
         ci = GetType(ErrMessageProperties). _
              GetConstructor(BindingFlags.NonPublic Or BindingFlags.Instance, _
                             Nothing, _
                             CallingConventions.ExplicitThis, _
                             New Type() {GetType(NumericInputBox)}, _
                             Nothing)

         Dim emp As ErrMessageProperties = CType(value, ErrMessageProperties)

         Dim ID As New InstanceDescriptor(ci, New Object() {emp.Parent})
         Return ID
      End If
      Return MyBase.ConvertTo(context, culture, value, destinationType)
   End Function

   Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
      If destinationType Is GetType(InstanceDescriptor) Then
         Return True
      End If
      If destinationType Is GetType(String) Then
         Return True
      End If
      Return MyBase.CanConvertTo(context, destinationType)
   End Function
End Class